<template>
  <div>

    <div class="loginbody">
      <div class="logo">
        <span class="title">创新创业项目管理系统</span>
        <span class="titleEn">Innovation and entrepreneurship project management system</span>
      </div>
      <div class="logindata">
        <div class="loginDiv">
          <h4 class="h4">欢迎登录</h4>
          <div class="formdata">
            <el-form ref="form" :model="form">
              <el-form-item prop="username">
                <div style="display: flex;width: 350px">
                  <span class="zhmm">账号：</span>
                  <el-input v-model="form.userCode" clearable placeholder="请输入学号/职工号"></el-input>
                </div>
              </el-form-item>
              <el-form-item prop="password">
                <div style="display: flex;width: 350px">
                  <span class="zhmm">密码：</span>
                  <el-input v-model="form.password" clearable placeholder="请输入密码" show-password></el-input>
                </div>
              </el-form-item>
            </el-form>
            <div class="tool">
              <div>
                <el-checkbox style="margin-right: 10px;" id="remember-me" v-model="rememberMe"
                             :checked="rememberMe"></el-checkbox>
                <label for="remember-me">记住密码</label>
              </div>
              <div>
                <el-button type="text" @click="addUserDialogVisible = true" class="zhuce">注册用户</el-button>
              </div>
            </div>
          </div>

          <div class="butt">
            <el-button type="primary" @click="login()">登录</el-button>
            <el-button @click="reset('form')">重置</el-button>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="用户注册" :visible.sync="addUserDialogVisible" width="40%">
      <el-form :model="newUser" :rules="rules" ref="newUser" :inline="true" class="demo-form-inline"
               label-position="right" label-width="100px">
        <el-form-item label="学号/职工号" prop="userCode">
          <el-input v-model="newUser.userCode" autocomplete="off" placeholder="请输入学号/职工号"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="userMc">
          <el-input v-model="newUser.userMc" autocomplete="off" placeholder="请输入姓名"></el-input>
        </el-form-item>
<!--        <el-form-item label="密码" prop="userMm">-->
<!--          <el-input type="password" v-model="newUser.userMm" placeholder="请输入密码"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="手机号" prop="userSjh">
          <el-input type="text" v-model="newUser.userSjh" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="userYx">
          <el-input type="email" v-model="newUser.userYx" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="学院" prop="userXy">
          <el-select ref="newUserXy" v-model="newUser.userXy" placeholder="请选择学院">
            <el-option v-for="item in xy" :key="item.keyname" :label="item.keyname" :value="item.keyvalue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="userYx">
          <el-input type="email" v-model="newUser.userBj" placeholder="请输入班级"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="userBm">
          <el-select ref="newUserBm" v-model="newUser.userBm" placeholder="请选择部门">
            <el-option v-for="item in bm" :key="item.keyname" :label="item.keyname" :value="item.keyvalue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="roleBh">
          <el-select v-model="newUser.roleBh" placeholder="请选择角色">
            <el-option v-for="item in roles" :key="item.roleBh" :label="item.roleMc" :value="item.roleBh">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addUser()">确 定</el-button>
        <el-button type="warning" @click="resetnewUserForm()">重 置</el-button>
        <el-button @click="addUserDialogVisible = false">取 消</el-button>
       </span>
    </el-dialog>
  </div>

</template>

<script>
import {insertUser, login} from "@/api/user"
import {selectRole} from "@/api/role";
import axios from "@/axios/axios";
import Cookies from 'js-cookie'

export default {
  data() {
    return {
      rememberMe: false,
      form: {
        password: "",
        userCode: "",
      },
      bm: [],
      xy: [],
      roles: [],
      addUserDialogVisible: false,
      newUser: {
        userMc: "",
        userMm: "",
        userSjh: "",
        userYx: "",
        userBm: "",
        userXy: "",
        roleBh: "",
        userBj: "",
        userCode: ""
      },
      rules: {
        userCode: [
          {required: true, message: '请输入学号/职工号', trigger: 'change'},
          {required: true, min: 12, max: 12, message: '请输入学号/职工号', trigger: 'change'}
        ],
        userMc: [
          {required: true, message: '请输入姓名', trigger: 'change'}
        ],
        userMm: [
          {required: true, message: '请填写密码', trigger: 'change'},
          {required: true, min: 6, message: '请输入6位以上密码', trigger: 'change'}
        ],
        userSjh: [
          {required: true, message: '请输入手机号', trigger: 'change'},
          {required: true, pattern: /^1[3456789]\d{9}$/, message: '请输入正确格式手机号', trigger: 'change'}
        ],
        userXy: [
          {required: true, message: '请选择学院', trigger: 'change'}
        ],
        roleBh: [
          {required: true, message: '请选择角色', trigger: 'change'}
        ],
      },
      dialogFormVisible: false,
    };
  },
  beforeCreate() {
    localStorage.clear();
  },
  created() {
    this.showRole()
    this.queryKey("xy,bm")
  },
  mounted() {
    const userCode = Cookies.get('userCode')
    const password = Cookies.get('password')

    if (userCode && password) {
      this.form.userCode = userCode
      this.form.password = password
      this.rememberMe = true
    }
  },
  methods: {
    login() {
      login(this.form).then(res => {
        console.log("log res", res)
        if (res.code == "200") {
          this.$message({
            message: res.msg,
            type: 'success'
          })

          localStorage.setItem("userName", res.data.userName)
          localStorage.setItem("userBh", res.data.user.userBh)
          localStorage.setItem("roleBh", res.data.user.roleBh)
          localStorage.setItem("avatarUrl", res.data.user.avatarUrl)
          localStorage.setItem("token", res.data.token)
          localStorage.setItem("menu_ids", res.data.menu_ids)
          localStorage.setItem("dzJks", res.data.dzJks)
          localStorage.setItem("xmBh", res.data.user.xmBh)
          localStorage.setItem("xmMc", res.data.user.xmMc)
          if (res.data.cxcyXm != null) {
            localStorage.setItem("xmKj", res.data.cxcyXm.xmKj)
            localStorage.setItem("xmzt", res.data.cxcyXm.xmzt)
            localStorage.setItem("xmKjhm", res.data.cxcyXm.xmKjhm)
          }

          this.$router.push("/home")
          if (this.rememberMe) {
            // 设置记住密码的 Cookie
            Cookies.set('userCode', this.form.userCode, {expires: 60 * 24 * 7 * 1000})
            Cookies.set('password', this.form.password, {expires: 60 * 24 * 7 * 1000})
          }
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    reset(formName) {
      this.$refs[formName].resetFields();
    },
    showRole() {
      var rolebhArr = ["R2002", "R2003", "R2004", "R2005", "R2007","R2008"]
      selectRole({
        keyWord: {
          rolemc: ""
        }
      }).then(res => {
        if (res.code == "200") {
          for (let role of res.data) {
            if (rolebhArr.indexOf(role.roleBh) !== -1) {
              this.roles.push(role)
            }
          }
        }
      })
    },
    queryKey(keyCodes) {
      axios({
        method: 'GET',
        url: '/keyValue/queryKey?keyCodes=' + keyCodes
      }).then(res => {
        if (res.code == "200") {
          this.xy = res.data.xy
          res.data.bm.forEach(bm => {
            if (bm.keyname == '全部') {
              bm.keyname = "无"
            }
          })
          this.bm = res.data.bm
        }
      })
    },
    addUser() {
      this.$refs['newUser'].validate((valid) => {
        if (valid) {
          insertUser(this.newUser).then(res => {
            if (res.code == "200") {
              this.$message({
                message: "申请成功，请等待管理员审批后进行登录！",
                type: "success"
              })
              this.newUser = {}
              this.addUserDialogVisible = false;
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    },
    resetnewUserForm() {
      this.$refs.newUser.resetFields();
    }
  }
}
</script>

<style scoped>
.zhmm {
  width: 80px;
  font-size: 20px;
  font-weight: bolder;
  color: white;
  text-shadow: 2px 2px 4px #000000;
}

.loginbody {
  width: 100%;
  height: 100%;
  min-width: 1000px;
  background-image: url("../assets/loginbg.png");
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  background-repeat: no-repeat;
  position: fixed;
  line-height: 100%;
//padding-top: 100px;
}


.formdata {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}

.logindata {
  width: 650px;
  height: 300px;
  position: relative;
  top: 10%;
  left: 78%;
  transform: translate(-50%);
  padding-top: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.tool {
  display: flex;
  justify-content: space-between;
  width: 100%;
  align-items: center;
  color: white;
  margin-top: 2rem;
}

.butt {
  display: flex;
  justify-content: space-around;
  width: 70%;
  align-items: center;
  margin-top: 2rem;
}


.loginDiv {
  z-index: 1;
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 70%;
  height: 100%;
  border: 1px solid #289CFF;
  background: linear-gradient(0deg, #22467D 0%, rgba(33, 140, 223, 0.3) 100%);
  box-shadow: 0px 0.01rem 0px 0px rgba(107, 107, 107, 0.27);
  border-radius: 0.7rem;
}

.logo {
  font-size: 3rem;
  font-family: Microsoft YaHei;
  font-weight: bold;
  position: relative;
  top: 5rem;
  right: 25%;
  letter-spacing: 0.04rem;
  color: #1576F7;
  height: 7rem;
  text-shadow: 0px 0.04rem 0.06rem rgba(30, 22, 21, 0);
  background: linear-gradient(0deg, #88C8FF 0%, #FFFFFF 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
//margin-left: 1.2rem;
}

.title {
  color: #2c4557;
  margin: 0 0;
}

.titleEn {
  color: #2c4557;
  margin: 0 0;
  font-size: 20px;
}

.loginDiv .h4 {
  padding-top: 0.15rem;
  letter-spacing: .2rem;
  font-size: 2rem;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #FFFFFF;
  line-height: 0.36rem;
  width: 100%;
  /* text-align: center; */
}

.zhuce {
  color: white;
  font-size: medium;
}

.zhuce :hover {
  color: #88C8FF;
}
</style>
